﻿// See https://aka.ms/new-console-template for more information
//创建连接工厂
using RabbitMQ.Client;
using System.Text;

ConnectionFactory factory = new ConnectionFactory()
{
    UserName = "admin",
    Password = "as103208900",
    HostName = "110.41.160.73",
    VirtualHost = "my_vhost"
};

//创建连接
using (var connection = factory.CreateConnection())
{
    //创建通道
    using (var channel = connection.CreateModel())
    {
        //声明一个队列
        channel.QueueDeclare("hello", false, false, false, null);
        //如果未声明交换机，则队列会自动绑定到默认direct类型的交换器，并以队列的形式作位路由键
        Console.WriteLine("rabbitmq连接成功，请输入消息，输入exit退出！");

        string input;
        do
        {
            input = Console.ReadLine();

            var sendBytes = Encoding.UTF8.GetBytes(input);
            //发布消息
            channel.BasicPublish("", "hello", null, sendBytes);
        } while (input.Trim().ToLower() != "exit");
    }
}